package com.woniu.outpatient.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.woniu.outpatient.domain.*;
import com.woniu.outpatient.service.IDoctorService;
import com.woniu.outpatient.service.impl.ChecksServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.outpatient.service.ICheckrecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 检查流水表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/checkrecord")
public class CheckrecordController extends BaseController {
    @Autowired
    private ICheckrecordService checkrecordService;
    @Autowired
    private IDoctorService iDoctorService;
    @Autowired
    private ChecksServiceImpl checksService;

    /**
     * 查询检查流水表列表
     */
    @RequiresPermissions("outpatient:checkrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(Checkrecord checkrecord) {
        startPage();
        List<Checkrecord> list = checkrecordService.selectCheckrecordList(checkrecord);
        return getDataTable(list);
    }


    @PostMapping("/MoneyTotal")
    public  TableDataInfo MoneyTotal(@RequestBody TimesVo timesVo){
        System.out.println(timesVo.getStartTime());
        List<MoneyTotalVO> moneyTotalVOS = checkrecordService.checkMoneyTotal(timesVo);
        return getDataTable(moneyTotalVOS);
    }

    @RequiresPermissions("outpatient:checkrecord:list")
    @GetMapping("/showAllTJ")
    public TableDataInfo showAllTJ(Checkrecord checkrecord) {
        startPage();
        List<Checkrecord> list = checkrecordService.selectCheckrecordList(checkrecord);
        return getDataTable(list);
    }

    @RequiresPermissions("outpatient:checkrecord:getDPCList")
    @GetMapping("/getDPCList")
    public TableDataInfo getDPCList() {
        List<Checks> checksList = checksService.selectChecksList(null);
        return getDataTable(checksList);
    }
    @RequiresPermissions("outpatient:checkrecord:getDPCList")
    @GetMapping("/getDPCListYan")
    public AjaxResult getDoctorListYan() {
        startPage();
        AjaxResult doctorListYan = iDoctorService.getDoctorListYan();
        return success(doctorListYan);
    }

    //这个方法跟检查表的查所有一模一样，只是调用的mapper不一样
    @RequiresPermissions("outpatient:CheckRecordVo:list")
    @GetMapping("/showAllYLL")
    public TableDataInfo showAllYLL(CheckRecordVo checkrecord) {
        System.out.println("tjj");
        startPage();
        List<CheckRecordVo> CheckRecordVoList = new ArrayList<CheckRecordVo>();
        Checkrecord checkrecord1 = new Checkrecord();
        checkrecord1.setCheckrecordId(checkrecord.getCheckrecordId());
        checkrecord1.setCheckrecordDesc(checkrecord.getCheckrecordDesc());
        checkrecord1.setCheckrecordResult(checkrecord.getCheckrecordResult());
        checkrecord1.setCheckrecordStatus(checkrecord.getCheckrecordStatus());
        checkrecord1.setChenckTime(checkrecord.getChenckTime());


        if(checkrecord.getCheckName() != null && checkrecord.getCheckName() != "") {
            checkrecord1.setCheckId(checksService.getIdByName(checkrecord.getCheckName()).getCheckId());
        }
        if(checkrecord.getDoctorName() != null && checkrecord.getDoctorName() != "") {
            checkrecord1.setDoctorId(iDoctorService.getDoctorIdByName(checkrecord.getDoctorName()).getDoctorId());
        }
        if(checkrecord.getPatientName() != null && checkrecord.getPatientName() != "") {
            checkrecord1.setpatientId(iDoctorService.getPatientIdByName(checkrecord.getPatientName()).getPatientId());
        }

        List<Checkrecord> list = checkrecordService.selectCheckrecordListYLL(checkrecord1);
        for (int i = 0; i < list.size(); i++) {
            CheckRecordVo checkRecordVo = new CheckRecordVo();
            checkRecordVo.setCheckrecordId(list.get(i).getCheckrecordId());
            checkRecordVo.setCheckrecordDesc(list.get(i).getCheckrecordDesc());
            checkRecordVo.setCheckrecordResult(list.get(i).getCheckrecordResult());
            checkRecordVo.setCheckrecordStatus(list.get(i).getCheckrecordStatus());
            checkRecordVo.setChenckTime(list.get(i).getChenckTime());
            checkRecordVo.setCheckId(list.get(i).getCheckId());
            checkRecordVo.setCheckName(checksService.selectChecksByCheckId(list.get(i).getCheckId()).getCheckName());
            checkRecordVo.setDoctorId(list.get(i).getDoctorId());
            checkRecordVo.setDoctorName(iDoctorService.selDoctorById(list.get(i).getDoctorId()).getDoctorName());
            checkRecordVo.setPatientId(list.get(i).getpatientId());
            checkRecordVo.setPatientName(iDoctorService.getpatientNameById(list.get(i).getpatientId()));
            CheckRecordVoList.add(checkRecordVo);
        }
        return getDataTable(CheckRecordVoList);
    }

    @RequiresPermissions("outpatient:CheckRecordVo:list")
    @GetMapping("/showAll")
    public TableDataInfo showAllYan(CheckRecordVo checkrecord) {
        startPage();
        List<CheckRecordVo> CheckRecordVoList = new ArrayList<CheckRecordVo>();
        Checkrecord checkrecord1 = new Checkrecord();
        checkrecord1.setCheckrecordId(checkrecord.getCheckrecordId());
        checkrecord1.setCheckrecordDesc(checkrecord.getCheckrecordDesc());
        checkrecord1.setCheckrecordResult(checkrecord.getCheckrecordResult());
        checkrecord1.setCheckrecordStatus(checkrecord.getCheckrecordStatus());
        checkrecord1.setChenckTime(checkrecord.getChenckTime());


        if(checkrecord.getCheckName() != null && checkrecord.getCheckName() != "") {
            checkrecord1.setCheckId(checksService.getIdByName(checkrecord.getCheckName()).getCheckId());
        }
        if(checkrecord.getDoctorName() != null && checkrecord.getDoctorName() != "") {
            checkrecord1.setDoctorId(iDoctorService.getDoctorIdByName(checkrecord.getDoctorName()).getDoctorId());
        }
        if(checkrecord.getPatientName() != null && checkrecord.getPatientName() != "") {
            checkrecord1.setpatientId(iDoctorService.getPatientIdByName(checkrecord.getPatientName()).getPatientId());
        }

        List<Checkrecord> list = checkrecordService.selectCheckrecordList(checkrecord1);
        for (int i = 0; i < list.size(); i++) {
            CheckRecordVo checkRecordVo = new CheckRecordVo();
            checkRecordVo.setCheckrecordId(list.get(i).getCheckrecordId());
            checkRecordVo.setCheckrecordDesc(list.get(i).getCheckrecordDesc());
            checkRecordVo.setCheckrecordResult(list.get(i).getCheckrecordResult());
            checkRecordVo.setCheckrecordStatus(list.get(i).getCheckrecordStatus());
            checkRecordVo.setChenckTime(list.get(i).getChenckTime());
            checkRecordVo.setCheckId(list.get(i).getCheckId());


            checkRecordVo.setCheckName(checksService.selectChecksByCheckId(list.get(i).getCheckId()).getCheckName());
            checkRecordVo.setDoctorId(list.get(i).getDoctorId());
            checkRecordVo.setDoctorName(iDoctorService.selDoctorById(list.get(i).getDoctorId()).getDoctorName());
            checkRecordVo.setPatientId(list.get(i).getpatientId());
            checkRecordVo.setPatientName(iDoctorService.getpatientNameById(list.get(i).getpatientId()));
            CheckRecordVoList.add(checkRecordVo);
        }
        return getDataTable(CheckRecordVoList);
    }

    /**
     * 导出检查流水表列表
     */
    @RequiresPermissions("outpatient:checkrecord:export")
    @Log(title = "检查流水表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Checkrecord checkrecord) {
        List<Checkrecord> list = checkrecordService.selectCheckrecordList(checkrecord);
        ExcelUtil<Checkrecord> util = new ExcelUtil<Checkrecord>(Checkrecord.class);
        util.exportExcel(response, list, "检查流水表数据");
    }

    /**
     * 获取检查流水表详细信息
     */
    @RequiresPermissions("outpatient:checkrecord:query")
    @GetMapping(value = "/{checkrecordId}")
    public AjaxResult getInfo(@PathVariable("checkrecordId") Long checkrecordId) {
        return success(checkrecordService.selectCheckrecordByCheckrecordId(checkrecordId));
    }


    @RequiresPermissions("outpatient:checkrecord:query")
    @GetMapping("/yan/{checkrecordId}")
    public AjaxResult getInfoYan(@PathVariable("checkrecordId") Long checkrecordId) {
        Checkrecord checkrecord = checkrecordService.selectCheckrecordByCheckrecordId(checkrecordId);

        CheckRecordVo checkRecordVo = new CheckRecordVo();
        checkRecordVo.setCheckrecordId(checkrecord.getCheckrecordId());
        checkRecordVo.setCheckrecordDesc(checkrecord.getCheckrecordDesc());
        checkRecordVo.setCheckrecordResult(checkrecord.getCheckrecordResult());
        checkRecordVo.setCheckrecordStatus(checkrecord.getCheckrecordStatus());
        checkRecordVo.setChenckTime(checkrecord.getChenckTime());
        checkRecordVo.setCheckId(checkrecord.getCheckId());
        checkRecordVo.setCheckName(checksService.selectChecksByCheckId(checkrecord.getCheckId()).getCheckName());
        checkRecordVo.setDoctorId(checkrecord.getDoctorId());
        checkRecordVo.setDoctorName(iDoctorService.getDoctorNameById(checkrecord.getDoctorId()));
        checkRecordVo.setPatientId(checkrecord.getpatientId());
        checkRecordVo.setPatientName(iDoctorService.getpatientNameById(checkrecord.getpatientId()));
        System.out.println("checkRecordVo = " + checkRecordVo);
        return success(checkRecordVo);
    }
    /**
     * 新增检查流水表
     */
    @RequiresPermissions("outpatient:checkrecord:add")
    @Log(title = "检查流水表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Checkrecord checkrecord) {
        return toAjax(checkrecordService.insertCheckrecord(checkrecord));
    }

    @RequiresPermissions("outpatient:checkrecord:add")
    @Log(title = "检查流水表", businessType = BusinessType.INSERT)
    @PostMapping("/addYan")
    public AjaxResult addYan(@RequestBody CheckRecordVo checkRecordVo) {
        System.out.println(checkRecordVo+"tjjj");
        Checkrecord checkrecord = new Checkrecord();
        checkrecord.setCheckrecordDesc(checkRecordVo.getCheckrecordDesc());
        checkrecord.setCheckrecordStatus("未缴费");
        checkrecord.setDoctorId(checkRecordVo.getDoctorId());
        checkrecord.setCheckrecordResult("未检查");
        checkrecord.setCheckId(checkRecordVo.getCheckId());
        checkrecord.setpatientId(checkRecordVo.getPatientId());
 //       checkrecord.setpatientId(iDoctorService.getPatientIdByName(checkRecordVo.getPatientName()).getPatientId());
        return toAjax(checkrecordService.insertCheckrecord(checkrecord));
    }
    /**
     * 修改检查流水表
     */
    @RequiresPermissions("outpatient:checkrecord:edit")
    @Log(title = "检查流水表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Checkrecord checkrecord) {
        return toAjax(checkrecordService.updateCheckrecord(checkrecord));
    }

    @RequiresPermissions("outpatient:checkrecord:edit")
    @Log(title = "检查流水表", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCheckRecordYan")
    public AjaxResult updateStatusResultById(@RequestBody CheckRecordVo checkrecordVo) {
        return toAjax(checkrecordService.updateStatusResultById(checkrecordVo));
    }

    /**
     * 删除检查流水表
     */
    @RequiresPermissions("outpatient:checkrecord:remove")
    @Log(title = "检查流水表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{checkrecordIds}")
    public AjaxResult remove(@PathVariable Long[] checkrecordIds) {
        return toAjax(checkrecordService.deleteCheckrecordByCheckrecordIds(checkrecordIds));
    }

}
